<template>
  <div v-show="$route.name == 'product-list'">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>肉码库存情况</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row class="query">
        <el-col :span="24">
          <el-select v-model="productSearch.category_id" placeholder="请选择分类" clearable popper-class="dropdownbox" filterable style="width: 150px;">
            <el-option v-for="item in categoryList" :key="item.id" :value="item.id" :label="item.category_name">
              <span style="float: left">{{ item.category_name }}</span>
            </el-option>
          </el-select>
          <el-button type="primary" @click="searchProduct" style="margin-left: 5px">查询</el-button>
          <el-button style="margin-left: 5px"  @click="reset">重置</el-button>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="24">
          <el-button type="success" @click="addProduct">添加成品</el-button>
        </el-col>
      </el-row> -->
      <el-table
        border
        v-loading="loading"
        :data="productData"
        style="margin-top: 10px;">
        <el-table-column label="ID" prop="id" min-width="80" align="center"></el-table-column>
        <el-table-column label="分类" prop="category" min-width="130" align="center"></el-table-column>
        <el-table-column label="商品名称" prop="name" min-width="160" align="center"></el-table-column>
        <el-table-column label="数量" prop="count" min-width="100" align="center"></el-table-column>
        <el-table-column label="成本" prop="price" min-width="100" align="center" v-if="groupid === 0 || groupid === 2"></el-table-column>
        <el-table-column label="单位" prop="unit" width="85" align="center"></el-table-column>
        <el-table-column label="操作" width="80" align="center" v-if="sellable">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="clickButton(scope.row.id)" v-if="scope.row.sellable && scope.row.count * 1 > 0">售出</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.currPage"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.totalCount">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      groupid: '',
      productSearch: {
        process: 1,
        category_id: ''
      },
      categoryList:[],
      loading: false,
      pager: {
        currPage: 1,
        pageSize: 10,
        totalCount: 0
      },
      productData: [],
      sellable: 0
    }
  },
  mounted() {
    // 获取当前时间
    const date = new Date()
    var hours = date.getHours()
    if (hours > 16) {
      this.sellable = 1
    }
    this.getCategoryList()
    this.getHouseList()
    this.getUserInfo()
  },
  methods: {
    // 获取用户信息
    async getUserInfo() {
      const { data: result } = await this.$http.get('/user/' + localStorage.userid)
      if (result.status !== 200) return this.$message.error(result.msg)
      this.groupid = result.data.groupid * 1
    },
    // 获取分类
    async getCategoryList() {
      const { data: result } = await this.$http.get('/category')
      if (result.status !== 200) return this.$message.error(result.msg)
      this.categoryList = result.data
    },
    async getHouseList() {
      this.loading = true
      const params = {
        page: this.pager.currPage,
        pageSize: this.pager.pageSize,
        category_id: this.productSearch.category_id,
        process: 1
      }
      const { data: result } = await this.$http.get('/house/list', { params: params })
      if (result.status !== 200) {
        this.loading = false
        this.productData = '暂无数据'
        return this.$message.error(result.msg)
      }
      this.loading = false
      this.productData = result.data
      this.pager = result.pager
    },
    // 查询
    searchProduct() {
      this.pager.currPage = 1
      this.getHouseList()
    },
    reset() {
      this.productSearch = {
        process: 1,
        category_id: '',
      },
      this.productData = []
      this.pager.currPage = 1
      this.getHouseList()
    },
    addProduct() {
      this.$router.push({ 'name': 'product-save' })
    },
    // 点击采购
    clickButton(id) {
      this.$router.push({
        name: 'product-sell',
        params: { id: id }
      })
    },
    handleSizeChange(pageSize) {
      this.pager.pageSize = pageSize
      this.getHouseList()
    },
    handleCurrentChange(currentPage) {
      this.pager.currPage = currentPage
      this.getHouseList()
    },
  }

}
</script>
<style lang="less">
  .query {
    margin-bottom: 20px;
  }
</style>